//
//  WPCPreviewViewController.m
//  Wallpaper.co
//
//  Created by zhou_yuepeng on 2017/9/6.
//  Copyright © 2017年 www.dahuatech.com. All rights reserved.
//

#import "WPCPreviewViewController.h"
#import "WPCPreviewPresenter.h"
#import "WPCThemesInfo.h"
#import "WPCPreviewView.h"
#import "WPCURLFactory.h"

@interface WPCPreviewViewController ()
@property (nonatomic, strong)   WPCPreviewPresenter *presenter;
@property (nonatomic, strong)   WPCPreviewView      *previewView;
@end

@implementation WPCPreviewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)initWithThemesItem:(WPCThemesItem*)item
{
    self = [super init];
    if (self) {
        self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0f];
        self.modalPresentationStyle = UIModalPresentationCustom;
        
        _presenter = [[WPCPreviewPresenter alloc] initWithThemesItem:item];
        
        CGRect frame = self.view.bounds;
        frame.origin.x += frame.size.width * 0.2;
        frame.size.width *= 0.8;
        
        _previewView = [[WPCPreviewView alloc] initWithFrame:frame];
        [_previewView setDelegate:(id<WPCPreviewViewDelegate>)_presenter];
        [_previewView setThumbnailURL:item.thumbnailURL originalPicURL:item.originalPicURL];
        [_previewView setTitle:item.title];
        [_previewView setIsFavorite:item.isFavorite.boolValue];
        
        [self.view addSubview:_previewView];
    }
    
    return self;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if (touch.view != self.view) {
        return;
    }
    
    [self dismissViewControllerAnimated:NO completion:nil];
}

@end
